package org.csu.pm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.csu.pm.common.BaseResponse;
import org.csu.pm.common.ErrorCode;
import org.csu.pm.common.ResultUtils;
import org.csu.pm.entity.Room;
import org.csu.pm.entity.User;
import org.csu.pm.entity.Userinfo;
import org.csu.pm.exception.BusinessException;
import org.csu.pm.mapper.RoomMapper;
import org.csu.pm.service.RoomService;

import org.csu.pm.service.UserService;
import org.csu.pm.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kwanho
 * @description 针对表【room】的数据库操作Service实现
 * @createDate 2022-11-11 20:16:25
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
        implements RoomService {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private UserService userService;

    @Override
    public BaseResponse<List<Room>> getRoomList() {
        List<Room> roomList = query().list();
        return ResultUtils.success(roomList);
    }

    @Override
    public BaseResponse<Room> updateRoom(Room room, Long id, Long userId) {
        if(!checkIfAdmin(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "当前登录用户不是管理员！无法更新会议室信息");
        }

        // room中的消息不能为空
        if (StringUtils.isAnyBlank(room.getLocation(), room.getTodo())) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "会议室信息不能为空");
        }

        // 把要查询的往后放 犯了上面的错误就可以减少查询数据库 提高效率
        Room query_room = query().eq("id", id).one();
        if (query_room == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "当前要更新的会议室不存在！");
        }

        room.setId(id);
        room.setStatus(query_room.getStatus());
        boolean ifSuccess = updateById(room);
        if(!ifSuccess) {
            throw new BusinessException(ErrorCode.SAVE_ERROR, "更新会议室失败,请稍后重试！");
        }

        return ResultUtils.success(room);
    }

    @Override
    public BaseResponse<Room> createRoom(Room room, Long id, Long userId) {
        if(!checkIfAdmin(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "当前登录用户不是管理员！无法新建会议室信息");
        }

        if(room.getId() == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "会议室id不能为空");
        }

        // room中的消息不能为空
        if (StringUtils.isAnyBlank(room.getLocation(), room.getTodo())) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "会议室信息不能为空");
        }

        // room中的id 必须和 请求路径中的id 保持一致
        if (room.getId() != id) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求路径中的id和输入会议室的id不一致！");
        }

        // 还要避免id 与 已有id 重复
        Room query_room = query().eq("id", id).one();
        if (query_room != null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "当前会议室id已被占用！");
        }


        // 会议室状态默认为1 可用
        room.setStatus(1);
        boolean ifSuccess = save(room);
        if(!ifSuccess) {
            throw new BusinessException(ErrorCode.SAVE_ERROR, "新增会议室失败,请稍后重试！");
        }

        return ResultUtils.success(room);
    }

    @Override
    public BaseResponse<String> deleteRoomById(Long id, Long userId) {
        // 1. 先查看当前登录用户是否为管理员
        if(!checkIfAdmin(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "当前登录用户不是管理员！无法删除");
        }

        // 2. 再查看该id的会议室是否存在
        Room room = query().eq("id", id).one();
        if(room == null) {
            // 3. 不存在则直接抛异常
            throw new BusinessException(ErrorCode.NULL_ERROR, "当前想删除的会议室不存在！");
        }

        // 4. 存在则返回成功信息
        boolean ifSuccess = removeById(id);
        if(!ifSuccess) {
            throw new BusinessException(ErrorCode.DELETE_ERROR, "删除当前会议室失败，请稍后重试！");
        }

        return ResultUtils.success("删除成功！");
    }

    @Override
    public BaseResponse<String> changeRoomStatus(Long id, Long userId) {
        // 1. 先查看当前登录用户是否为管理员
        if(!checkIfAdmin(userId)) {
            throw new BusinessException(ErrorCode.NO_AUTH, "当前登录用户不是管理员！无法删除");
        }

        // 2. 再查看该id的会议室是否存在
        Room room = query().eq("id", id).one();
        if(room == null) {
            // 3. 不存在则直接抛异常
            throw new BusinessException(ErrorCode.NULL_ERROR, "当前想删除的会议室不存在！");
        }

        // 如果当前会议室正在被使用
        if(room.getStatus() == 2) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR, "当前会议室正在被使用，请稍后再修改");
        }else {
            // 否则则翻转状态
            room.setStatus(room.getStatus() == 0 ? 1 : 0);
        }

        boolean ifSuccess = updateById(room);
        if(!ifSuccess) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR, "修改会议室状态失败，请稍后重试！");
        }

        return ResultUtils.success("修改会议室状态成功！");
    }

    @Override
    public BaseResponse<Room> getRoomById(Long id) {
        Room room = query().eq("id", id).one();
        if(room == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "当前查询会议室不存在！");
        }
        return ResultUtils.success(room);
    }

    private boolean checkIfAdmin(Long userId) {
        User user = userService.query().eq("id", userId).one();
        Userinfo userinfo = userinfoService.query().eq("stu_no", user.getStu_no()).one();
        // 为普通用户
        if(userinfo.getUser_type() == 0) {
            return false;
        }
        return true;
    }



}




